#!/usr/bin/env bash

# curl -fsSL http://dev8.cn:8000/static/ops/k8s1-ip.sh | bash
# https://www.cnblogs.com/chaojiyingxiong/p/12106590.html

old_ip=$1
new_ip=$2
if [ -z "$old_ip" ]
then
  echo "必须指定老的ipv4格式的k8s master ip"
  exit 1
fi

if [ -z "$new_ip" ]
then
  echo "必须指定新的ipv4格式的k8s master ip"
  exit 1
fi

sed -i "s/$old_ip/$new_ip/" /etc/kubernetes/manifests/etcd.yaml
sed -i "s/$old_ip/$new_ip/" /etc/kubernetes/manifests/kube-apiserver.yaml
sed -i "s/$old_ip/$new_ip/" /etc/kubernetes/admin.conf
cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
chown $(id -u):$(id -g) $HOME/.kube/config

cd /etc/kubernetes
kubeadm init phase kubeconfig admin --apiserver-advertise-address $new_ip

rm -f /etc/kubernetes/pki/apiserver.key
rm -f /etc/kubernetes/pki/apiserver.crt

cd /etc/kubernetes/pki
kubeadm init phase certs apiserver  --apiserver-advertise-address $new_ip

service docker restart
service kubelet restart

kubectl get nodes --kubeconfig=admin.conf


